package com.sy.controller;

import com.sy.dto.CollectDto;
import com.sy.dto.JsonResult;
import com.sy.dto.MemberDto;
import com.sy.entity.Member;
import com.sy.exception.BusinessException;
import com.sy.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

import static com.sy.constant.BusinessConstant.SESSION_VALIDATE_CODE;

/**
 * @author easonsy
 * @Description
 * @createTime 2022/06/24 15:40:00
 */
@RestController
@RequestMapping("collect")
public class CollectController {
    @Autowired
    private CollectService collectService;

    @RequestMapping("show")
    public List<CollectDto> showCollect(HttpSession session) {
        if (session.getAttribute("member")==null){
            throw new BusinessException("用户未登录");
        }
        MemberDto member=(MemberDto) session.getAttribute("member");
        return collectService.getAllCollects(member.getId());
    }

    @RequestMapping("add")
    public JsonResult addCollect(Integer subjectId, HttpSession session) {
        if (session.getAttribute("member")==null){
            return new JsonResult().setData(404).setErrMsg("用户未登录");
        }
        MemberDto member=(MemberDto) session.getAttribute("member");
        if (collectService.addCollects(member.getId(), subjectId)) {
            return new JsonResult().setCode(200).setData("收藏成功");
        } else {
            return new JsonResult().setCode(404).setErrMsg("已添加收藏");
        }

    }

    @RequestMapping("del")
    public JsonResult delCollect( Integer subjectId,HttpSession session) {
        if (session.getAttribute("member")==null){
            return new JsonResult().setData(404).setErrMsg("用户未登录");
        }
        MemberDto member=(MemberDto) session.getAttribute("member");
        if (collectService.delCollects(member.getId(), subjectId)) {
            return new JsonResult().setCode(200).setData("删除成功");
        } else {
            return new JsonResult().setCode(404).setErrMsg("删除失败");
        }
    }
}
